package com.wen.web.init;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.view.View;

import com.tencent.smtt.sdk.WebChromeClient;
import com.tencent.smtt.sdk.WebSettings;
import com.tencent.smtt.sdk.WebView;
import com.tencent.smtt.sdk.WebViewClient;


/**
 * WEbView初始化的定义
 */

public class WebViewInitializer implements IWebViewInitializer {

    private static final String USER_AGENT = "appCarB";
    public Activity activity;
    private WebChromeClientImpl.OpenFileChooserListener listener;
    private WebChromeClientImpl client;

    public WebViewInitializer(Activity activity) {
        this(activity, null);
    }

    public WebViewInitializer(Activity activity, WebChromeClientImpl.OpenFileChooserListener listener) {
        this.activity = activity;
        this.listener = listener;
    }

    @Override
    public WebView initWebView(WebView webView) {
        webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
        WebSettings webSetting = webView.getSettings();
        webSetting.setUserAgentString(USER_AGENT + webSetting.getUserAgentString());

        webSetting.setDatabaseEnabled(true);
        webSetting.setRenderPriority(WebSettings.RenderPriority.HIGH);
        webSetting.setTextSize(WebSettings.TextSize.NORMAL);

        // ===设置JS可用
        webSetting.setJavaScriptEnabled(true);
        // JS打开窗口
        webSetting.setJavaScriptCanOpenWindowsAutomatically(true);
        // ===设置JS可用
        // 可以访问文件
        webSetting.setAllowFileAccess(true);
        // ===缩放可用
        webSetting.setSupportZoom(true);
        webSetting.setDisplayZoomControls(false); //隐藏原生的缩放控件
        webSetting.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS); //设置缩放功能   //能不能缩放 取决于网页设置
        webSetting.setLoadWithOverviewMode(true);
        webSetting.setBuiltInZoomControls(true);
        // ===缩放可用
        // 支持多窗口
        webSetting.setSupportMultipleWindows(true);
        // ===============缓存
        webSetting.setCacheMode(WebSettings.LOAD_DEFAULT);// 决定是否从网络上取数据。
        webSetting.setAppCacheEnabled(true);
        // ===============缓存
        webSetting.setUseWideViewPort(true);
        webSetting.setAppCacheMaxSize(Long.MAX_VALUE);
        webSetting.setPluginState(WebSettings.PluginState.ON_DEMAND);
        // ==定位
        webSetting.setDomStorageEnabled(true);
        webSetting.setGeolocationEnabled(true);
        // ==定位
        return webView;
    }

    @Override
    public WebViewClient initWebViewClient() {
        WebViewClient client = new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (view != null && url != null) {
                    if (url.endsWith(".apk")) {
                        Uri apkUri = Uri.parse(url);
                        Intent intent = new Intent(Intent.ACTION_VIEW, apkUri);
                        view.getContext().startActivity(intent);
                    } else if (url.startsWith("http")) {
                        view.loadUrl(url);
                    } else {
                        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                        if (isInstall(view.getContext(), intent)) {
                            view.getContext().startActivity(intent);
                        }
                    }
                }
                return true;
            }

        };
        return client;
    }

    //判断app是否安装
    private boolean isInstall(Context context, Intent intent) {
        return context.getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY).size() > 0;
    }

    @Override
    public WebChromeClient initWebChromeClient() {
        client = new WebChromeClientImpl();
        client.setOpenFileChooserListener(listener);
        return client;
    }

    public void setOnWebChromeListener(WebChromeClientImpl.OnWebChromeListener onWebChromeListener) {
        client.setOnWebChromeListener(onWebChromeListener);
    }

    /**
     * 设置打开文件的监听
     *
     * @param listener
     */
    public void setOpenFileChooser(WebChromeClientImpl.OpenFileChooserListener listener) {
        this.listener = listener;
    }
}
